import Vue from 'vue'

import Cookies from 'js-cookie'

import 'normalize.css/normalize.css' // a modern alternative to CSS resets

import Element from 'element-ui'
import './styles/element-variables.scss'

import '@/styles/index.scss' // global css

import App from './App'
import store from './store'
import router from './router'

import './icons' // icon
import './permission' // permission control
import './utils/error-log' // error log
import './utils/calendars'

import * as filters from './filters' // global filters
import formRules from './utils/formRules' // global filters
import request from './utils/request'
import echarts from 'echarts'
import svga from 'vue-svga'
import { hasPermission } from './utils/btnPermission.js' // 按钮权限指令

Vue.use(hasPermission) // 按钮权限指令
Vue.use(svga)
Vue.prototype.$echarts = echarts;

/**
 * If you don't want to use mock-server
 * you want to use MockJs for mock api
 * you can execute: mockXHR()
 *
 * Currently MockJs will be used in the production environment,
 * please remove it before going online! ! !
 */
// import { mockXHR } from '../mock'
if (process.env.NODE_ENV === 'production') {
  // mockXHR()
}

Vue.use(Element, {
  size: Cookies.get('size') || 'medium' // set element-ui default size
})
// 表单参数校验规则
Vue.prototype.formRules = formRules
Vue.prototype.cfg = {
  apiDomain: process.env.VUE_APP_BASE_API,
  imageDomain: process.env.VUE_APP_IMAGE_BASE_API
}
/**
 * 防按钮重复提交
 * @param vue
 * @param key
 */
Vue.prototype.disableBtn = (vue, key) => {
  vue[key] = true;
  setTimeout(() => {
    vue[key] = false;
  }, 3000);
}
Vue.prototype.getUrlParam = key => {
  const re = new RegExp('[?|&]' + key + '=(.*?)(&|$|#)(#?)');
  const r = window.location.href.match(re);
  if (r != null) return decodeURIComponent(r[1]);
  return null;
}

// register global utility filters
Object.keys(filters).forEach(key => {
  Vue.filter(key, filters[key])
})

/**
 * 将字典设置到Vue的过滤器中
 * @param name
 */
Vue.prototype.registerFilter = function(name) {
  request({
    url: `/common/dict/getDictDataByName/${name}`
  }).then(function(res) {
    const data = res.data;
    Vue.filter('filter' + name, function(value) {
      for (let i = 0; i < data.length; i++) {
        if (data[i].code + '' === value + '') {
          return data[i].text;
        }
      }
      return value;
    })
  });
}
/**
 * 字符长度过滤
 */
Vue.prototype.charFilter = function(length) {
  Vue.filter('charFilter', function(value) {
    if (!value) return '';
    if (value.length > length) {
      return value.slice(0, length) + '...';
    }
    return value;
  })
}

Vue.config.productionTip = false
new Vue({
  el: '#app',
  router,
  store,
  render: h => h(App)
})
